package cn.edu.ctbu.dp01.controller;

import cn.edu.ctbu.dp01.common.Result;
import cn.edu.ctbu.dp01.entity.Student;
import cn.edu.ctbu.dp01.exception.BusinessException;
import cn.edu.ctbu.dp01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/students")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    // 获取所有学生
    @GetMapping
    public Result<List<Student>> getAllStudents() {
        List<Student> students = studentService.findAll();
        return Result.success(students, (long) students.size());
    }

    // 根据ID获取学生
    @GetMapping("/{id}")
    public Result<Student> getStudentById(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        if (student == null) {
            throw new BusinessException("未找到ID为" + id + "的学生");
        }
        return Result.success(student);
    }

    // 添加学生
    @PostMapping
    public Result<Student> addStudent(@RequestBody Student student) {
        // 参数校验
        if (student.getName() == null || student.getName().trim().isEmpty()) {
            throw new BusinessException("学生姓名不能为空");
        }
        if (student.getSno() == null || student.getSno().trim().isEmpty()) {
            throw new BusinessException("学号不能为空");
        }
        if (student.getAge() == null || student.getAge() < 0) {
            throw new BusinessException("年龄不能为空且必须为正数");
        }
        
        Student savedStudent = studentService.save(student);
        return Result.success(savedStudent);
    }

    // 更新学生
    @PutMapping("/{id}")
    public Result<Student> updateStudent(@PathVariable Integer id, @RequestBody Student student) {
        // 验证学生是否存在
        Student existingStudent = studentService.findById(id);
        if (existingStudent == null) {
            throw new BusinessException("未找到ID为" + id + "的学生");
        }
        
        // 参数校验
        if (student.getName() == null || student.getName().trim().isEmpty()) {
            throw new BusinessException("学生姓名不能为空");
        }
        if (student.getSno() == null || student.getSno().trim().isEmpty()) {
            throw new BusinessException("学号不能为空");
        }
        if (student.getAge() == null || student.getAge() < 0) {
            throw new BusinessException("年龄不能为空且必须为正数");
        }
        
        student.setId(id);
        Student updatedStudent = studentService.save(student);
        return Result.success(updatedStudent);
    }

    // 删除学生
    @DeleteMapping("/{id}")
    public Result<Object> deleteStudent(@PathVariable Integer id) {
        // 验证学生是否存在
        Student existingStudent = studentService.findById(id);
        if (existingStudent == null) {
            throw new BusinessException("未找到ID为" + id + "的学生");
        }
        
        studentService.deleteById(id);
        return Result.success("删除成功");
    }
}